/**
 * 
 */
package xiaojian.toolkit.transport.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author isdom
 *
 */
public class MinaTextEncoder implements ProtocolEncoder  {
    
    private static final Logger logger = LoggerFactory.getLogger(MinaTextEncoder.class);
    private int dumpBytes = 256;
    private	boolean	isDebugEnabled;

    private	static final String	ENCODING = "UTF-8";
    
	/* (non-Javadoc)
     * @see org.apache.mina.filter.codec.ProtocolEncoder#encode(org.apache.mina.core.session.IoSession, java.lang.Object, org.apache.mina.filter.codec.ProtocolEncoderOutput)
     */
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
            throws Exception {

    	if ( message instanceof String ) {
    		byte[] bytes = ((String)message).getBytes(ENCODING);
            if ( null != bytes ) {
                out.write( IoBuffer.wrap(bytes) );
            }
        }
        else {
            logger.error("encode: " + message + " is not String.");
        }
    }

	public boolean isDebugEnabled() {
		return isDebugEnabled;
	}

	public void setDebugEnabled(boolean isDebugEnabled) {
		this.isDebugEnabled = isDebugEnabled;
	}

	public int getDumpBytes() {
		return dumpBytes;
	}
	
    /**
     * @param dumpBytes the dumpBytes to set
     */
    public void setDumpBytes(int dumpBytes) {
        this.dumpBytes = dumpBytes;
    }

	public void dispose(IoSession session) throws Exception {
	}
}
